﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Hi_Baidu.Net
{
    public abstract class PortListener : IListener
    {
        /// <summary>
        ///     端口监听器
        /// </summary>
        /// <param name="Port" type="int">
        ///     <para>
        ///         要监听的端口号
        ///     </para>
        /// </param>
        /// <param name="messageListener" type="UM_SERVER.Net.MessageListener">
        ///     <para>
        ///         负责接受消息到此端口的消息接收器
        ///     </para>
        /// </param>
        protected PortListener(int Port, MessageListener messageListener, String Key)
        {
            this.Port = Port;
            this.Listener = messageListener;
            this.key = Key;
        }

        /// <summary>
        ///     是否循环监听的标志位
        /// </summary>
        protected bool IS_LOOP_LISTEN = true;
        /// <summary>
        ///     当前监听端口的线程
        /// </summary>
        protected Thread CurrentThread = null;
        /// <summary>
        ///     消息接收器
        /// </summary>
        protected MessageListener Listener = null;
        /// <summary>
        ///     要监听的端口号
        /// </summary>
        protected int Port = 0;

        public MessageListener GetMessageListener()
        {
            return this.Listener;
        }

        #region 'OnUserConnected' event definition code

        /// <summary>
        ///     EventArgs derived type which holds the custom event fields
        /// </summary>
        public class OnUserConnectedEventArgs : System.EventArgs
        {
            /// <summary>
            ///     Use this constructor to initialize the event arguments
            ///     object with the custom event fields
            /// </summary>
            public OnUserConnectedEventArgs(Socket socUserSocket)
            {
                this.UserSocket = socUserSocket;
            }

            /// <summary>
            ///     TODO: Describe the purpose of this event argument here
            /// </summary>
            public readonly Socket UserSocket;

        }

        // Private delegate linked list (explicitly defined)
        private EventHandler<OnUserConnectedEventArgs> OnUserConnectedEventHandlerDelegate;

        /// <summary>
        ///     TODO: Describe the purpose of this event here
        /// </summary>
        public event EventHandler<OnUserConnectedEventArgs> OnUserConnected
        {
            // Explicit event definition with accessor methods
            add
            {
                OnUserConnectedEventHandlerDelegate = (EventHandler<OnUserConnectedEventArgs>)Delegate.Combine(OnUserConnectedEventHandlerDelegate, value);
            }
            remove
            {
                OnUserConnectedEventHandlerDelegate = (EventHandler<OnUserConnectedEventArgs>)Delegate.Remove(OnUserConnectedEventHandlerDelegate, value);
            }
        }

        /// <summary>
        ///     This is the method that is responsible for notifying
        ///     receivers that the event occurred
        /// </summary>
        protected virtual void OnOnUserConnected(OnUserConnectedEventArgs e)
        {
            if (OnUserConnectedEventHandlerDelegate != null)
            {
                OnUserConnectedEventHandlerDelegate(this, e);
            }
        }

        #endregion //('OnUserConnected' event definition code)

        #region IListener 成员

        /// <summary>
        ///     监听初始化动作
        /// </summary>
        public abstract void Init();

        /// <summary>
        ///     开始监听指定端口
        /// </summary>
        /// <returns>
        ///     返回true ， 则表示监听成功
        /// </returns>
        public bool Start()
        {
            this.IS_LOOP_LISTEN = true;
            Init();
            return true;
        }

        /// <summary>
        ///     停止监听指定端口
        /// </summary>
        /// <returns>
        ///     返回true ， 则表示停止成功
        /// </returns>
        public bool Stop()
        {
            if(this.IS_LOOP_LISTEN)
            {
                this.IS_LOOP_LISTEN = false;
                this.Dispose();
            }

            return true;
        }

        /// <summary>
        ///     更改监听状态
        /// </summary>
        /// <param name="State" type="bool">
        ///     <para>
        ///         监听状态
        ///     </para>
        /// </param>
        public void ChanageState(bool State)
        {
            this.IS_LOOP_LISTEN = State;
        }

        /// <summary>
        ///     获得当前监听端口的线程
        /// </summary>
        /// <returns>
        ///     返回当前监听端口的线程
        /// </returns>
        public Thread GetCurrentThread()
        {
            return this.CurrentThread;
        }

        #endregion

        #region IMetaData 成员

        private String key;
        /// <summary>
        ///     唯一标示
        /// </summary>
        public string Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
            }
        }

        #endregion

        #region IListener 成员

        /// <summary>
        ///     停止监听之后的处理
        /// </summary>
        public abstract void Dispose();

        #endregion

    }
}
